package com.orange.api;

import server.rest.HttpContext;
import server.rest.HttpMethod;
import server.rest.Rest;
import server.rest.filter.Filter;
import common.BaseUtils;
import common.CacheManager;
import common.html.DefaultTag;
import common.html.LinkTag;
import common.html.ScriptTag;
import common.html.Tag;
import common.html.Tag.PageTag;
import common.html.TagFactory;

public class PageHandler
{
    static DefaultTag meta =
                    TagFactory.createTag("meta", new String[][] { { "http-equiv", "Content-Type" },
                                    { "content", "text/html; charset=utf-8" } });

    @Filter(name = "Auth", filter = "com.orange.api.filter.AuthFilter")
    @Rest(method = HttpMethod.GET, schema = "/frame", internal = false, appSchame = "/page")
    public void doGetFrame(HttpContext context)
    {
    		PageTag pt = new PageTag();
	        ScriptTag scr = TagFactory.createScript("/js/sea.js");
	        LinkTag css1 = TagFactory.createLink("../js/base/assets/css/bootstrap.min.css");
	        LinkTag css2 = TagFactory.createLink("../js/base/assets/css/bootstrap-responsive.min.css");
	        LinkTag css3 = TagFactory.createLink("../js/base/assets/css/font-awesome.min.css");
	        LinkTag css4 = TagFactory.createLink("../js/base/assets/css/font-awesome-ie7.min.css");
	        LinkTag css5 = TagFactory.createLink("../js/base/assets/css/ace-fonts.css");
	        LinkTag css6 = TagFactory.createLink("../js/base/assets/css/ace.min.css");
	        LinkTag css7 = TagFactory.createLink("../js/base/assets/css/ace-responsive.min.css");
	        LinkTag css8 = TagFactory.createLink("../js/base/assets/css/ace-skins.min.css");
	        pt.addHeaderElement(getTitle("校训后台管理系统"), meta, scr, css1, css2, css3, css4, css5, css6, css7, css8);
	
	        ScriptTag seaModule = TagFactory.createScript();
	        seaModule.apped(" seajs.config({ 'base': '/js/base' });");
	        seaModule.apped(" seajs.use('modules/frame.page', function (app) { app.init();});");
	        pt.addBodayElement(  TagFactory.createTag("div", new String[][]{{"id","frame"}}),seaModule);
	        context.sendHtml(pt.getContentWithOpenAndCloseTag());
    }

    DefaultTag getTitle(String title)
    {
        return TagFactory.createTag("title", title);
    }
    
    @Rest(method = HttpMethod.GET, schema = "/login", internal = false, appSchame = "/page")
    public void loginPage(HttpContext context)
    {
        String token = BaseUtils.encodeForBase64(String.format("%s-%s-%s", context.getRequest().getRemoteAddr(),
                context.getRequest().getRequestedSessionId(),
                context.getRequest().getHeader("User-Agent")));
    	
    	String loginHtml = CacheManager.getCacheInfo("login").getValue().toString();
    	String tokenTag = "#tokenValue";
    	if(loginHtml.contains(tokenTag))
    	{
    		loginHtml = loginHtml.replace(tokenTag, token);
    	}
        context.sendHtml(loginHtml);
    }

    private Tag inputTg(String name, String displayName)
    {
        return inputTg(name, displayName, "text");
    }

    private Tag button(String id, String context)
    {
        DefaultTag btn = TagFactory.createTag("button", context);
        btn.putAttr("id", id).putAttr("type", "button").putclass("btn btn-default");
        return btn;
    }

    private Tag wrapDiv(Tag... tags)
    {
        return wrapDiv("form-group", tags);
    }

    private Tag wrapDiv(String className, Tag... tags)
    {
        DefaultTag divTg = TagFactory.createTag("div");
        divTg.content(tags);
        divTg.putclass(className);
        return divTg;
    }

    private Tag inputTg(String name, String displayName, String type)
    {

        DefaultTag label = TagFactory.createTag("label");
        label.putAttr("for", name);
        label.content(displayName);

        DefaultTag input = TagFactory.createTag("input");
        input.putAttr("type", type).putAttr("name", name).putAttr("id", name).putAttr("placeholder", displayName).putclass(
            "form-control");

        return wrapDiv(label, input);
    }
}
